package com.yellowpage.omc.o2o.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.yellowpage.white.common.Configuration;
import com.yellowpage.white.common.Constants;
import com.yellowpage.white.struts.BaseAction;
import com.yellowpage.white.struts.Module;
import com.yellowpage.white.struts.OperationType;
import com.yellowpage.white.util.Util4Java;

import com.yellowpage.omc.o2o.data.CustomerExtInfo;
import com.yellowpage.omc.o2o.service.CustomerExtInfoService;

public class CustomerExtInfoAction extends BaseAction {

	public String RESULT_QUERY = "query";
	public String RESULT_ADD = "add";
	public String RESULT_EDIT = "edit";

	private Configuration configuration;

	public void setConfiguration(Configuration configuration) {
		this.configuration = configuration;
	}

	private List<CustomerExtInfo> customerExtInfoList = new ArrayList<CustomerExtInfo>();
	private List<Map> resultList = new ArrayList<Map>();

	private CustomerExtInfo customerExtInfo;
	private CustomerExtInfoService customerExtInfoService;

	private String selectedId;
	private String[] selectedIds;
	private String message;

	private String query_customerExtInfoId;
	private String query_customerId;
	private String query_extInfoId;
	private String query_createUser;
	private String query_createTime;
	private String query_lastUpdateUser;
	private String query_lastUpdateTime;
	private String query_value;

	public void prepare() {
		super.prepare();

		if (this.selectedId != null && !this.selectedId.equals("")) {
			this.customerExtInfo = customerExtInfoService
					.getCustomerExtInfo(Long.parseLong(selectedId));
		}
	}

	public String queryByPage() {
		initPageRender();

		Map queryMap = new HashMap();
		queryMap.put("customerExtInfoId", query_customerExtInfoId);
		queryMap.put("customerId", query_customerId);
		queryMap.put("extInfoId", query_extInfoId);
		queryMap.put("createUser", query_createUser);
		queryMap.put("createTime", query_createTime);
		queryMap.put("lastUpdateUser", query_lastUpdateUser);
		queryMap.put("lastUpdateTime", query_lastUpdateTime);
		queryMap.put("value", query_value);

		resultList = customerExtInfoService.queryCustomerExtInfoByPage(
				queryMap, pageRender);

		setPageRenderToRequest();

		return RESULT_QUERY;
	}

	public String toAdd() {
		return RESULT_ADD;
	}

	public String save() {
		customerExtInfo.setCreateUser(Integer.valueOf(loginUser.getUserId().toString()));
		customerExtInfo.setCreateTime(new Date());
		customerExtInfoService.saveCustomerExtInfo(customerExtInfo);

		message = "新增成功!";
		return RESULT_ADD;
	}

	public void validateSave() {
		if (customerExtInfo.getValue().length() == 0) {
			this.addActionError("** 对应值 不能为空 !");
		}
		if (customerExtInfo.getCustomerId() == null) {
			this.addActionError("客户编号 不能为空 !");
		}
		if (customerExtInfo.getExtInfoId() == null) {
			this.addActionError("扩展信息表编号 不能为空 !");
		}

		this.setValidatorResult("/omc/o2o/CustomerExtInfoAdd.jsp");
	}

	public String toEdit() {
		Long id = new Long(selectedId);

		customerExtInfo = customerExtInfoService.getCustomerExtInfo(id);

		return RESULT_EDIT;
	}

	public String update() {
		customerExtInfo.setLastUpdateUser(Integer.valueOf(loginUser.getUserId().toString()));
		customerExtInfo.setLastUpdateTime(new Date());
		customerExtInfoService.updateCustomerExtInfo(customerExtInfo);

		message = "修改成功!";

		return RESULT_EDIT;
	}

	public void validateUpdate() {
		if (customerExtInfo.getValue().length() == 0) {
			this.addActionError("** 对应值 不能为空 !");
		}
		if (customerExtInfo.getCustomerId() == null) {
			this.addActionError("客户编号 不能为空 !");
		}
		if (customerExtInfo.getExtInfoId() == null) {
			this.addActionError("扩展信息表编号 不能为空 !");
		}
		this.setValidatorResult("/omc/o2o/CustomerExtInfoUpdate.jsp");
	}

	public String delete() {
		List ids = new ArrayList();

		for (String id : selectedIds) {
			ids.add(Long.valueOf(id));
		}

		try {
			this.customerExtInfoService.delete(ids);
		} catch (Exception e) {
			System.out.println("异常....");
			this.addActionError("** 可能含有子记录约束冲�?删除失败 !");
		}

		return queryByPage();
	}

	/**
	 * getters and setters
	 * 
	 * @return
	 */
	public void setCustomerExtInfo(CustomerExtInfo customerExtInfo) {
		this.customerExtInfo = customerExtInfo;
	}

	public CustomerExtInfo getCustomerExtInfo() {
		return customerExtInfo;
	}

	public void setCustomerExtInfoService(
			CustomerExtInfoService customerExtInfoService) {
		this.customerExtInfoService = customerExtInfoService;
	}

	public List<CustomerExtInfo> getCustomerExtInfoList() {
		return customerExtInfoList;
	}

	public void setCustomerExtInfoList(List<CustomerExtInfo> customerExtInfoList) {
		this.customerExtInfoList = customerExtInfoList;
	}

	public String getSelectedId() {
		return selectedId;
	}

	public void setSelectedId(String selectedId) {
		this.selectedId = selectedId;
	}

	public String[] getSelectedIds() {
		return selectedIds;
	}

	public void setSelectedIds(String[] selectedIds) {
		this.selectedIds = selectedIds;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public List<Map> getResultList() {
		return resultList;
	}

	public void setResultList(List<Map> resultList) {
		this.resultList = resultList;
	}

	public String getQuery_customerExtInfoId() {
		return this.query_customerExtInfoId;
	}

	public void setQuery_customerExtInfoId(String customerExtInfoId) {
		this.query_customerExtInfoId = customerExtInfoId;
	}

	public String getQuery_customerId() {
		return this.query_customerId;
	}

	public void setQuery_customerId(String customerId) {
		this.query_customerId = customerId;
	}

	public String getQuery_extInfoId() {
		return this.query_extInfoId;
	}

	public void setQuery_extInfoId(String extInfoId) {
		this.query_extInfoId = extInfoId;
	}

	public String getQuery_createUser() {
		return this.query_createUser;
	}

	public void setQuery_createUser(String createUser) {
		this.query_createUser = createUser;
	}

	public String getQuery_createTime() {
		return this.query_createTime;
	}

	public void setQuery_createTime(String createTime) {
		this.query_createTime = createTime;
	}

	public String getQuery_lastUpdateUser() {
		return this.query_lastUpdateUser;
	}

	public void setQuery_lastUpdateUser(String lastUpdateUser) {
		this.query_lastUpdateUser = lastUpdateUser;
	}

	public String getQuery_lastUpdateTime() {
		return this.query_lastUpdateTime;
	}

	public void setQuery_lastUpdateTime(String lastUpdateTime) {
		this.query_lastUpdateTime = lastUpdateTime;
	}

	public String getQuery_value() {
		return this.query_value;
	}

	public void setQuery_value(String value) {
		this.query_value = value;
	}

}
